// pages/employees/join/index.js
import requestUtil from '../../../baseRequest'
import urls from '../../../requestUrl'


Page({

    /**
     * 页面的初始数据
     */
    data: {
        joinInfo: {
            "driverNum": 0,
            "employeeName": "",
            "companyName": ""
        },
        userStatus: 0,
        spinShow: false
    },

    /**点击立即加入按钮,获取用户信息*/
    handleGetUserInfo({ detail }) {
        if (detail.userInfo) {
            this.updateUserInfo(detail.userInfo)
        } else {
            wx.showModal({
                title: '授权加入团队失败',
                content: '授权加入失败，无法获取您的公开信息(昵称，头像等),请重新授权登陆',
                showCancel: false
            })
        }
    },

    /**更新用户授权信息 */
    updateUserInfo(info) {
        console.log(info)
        if (!this.data.openid) {
            this.getUserOpenid()
        }
        let param = {
            "headimgUrl": info.avatarUrl,
            "nickname": info.nickName,
            "openid": this.data.openid
        }
        requestUtil.postRequest(urls.user.updateInfo, param, (res) => {
            getApp().updateGlobalUserInfo(1, info.avatarUrl, info.nickName)
            this.employeeToJoin()
        }, () => {})
    },

    /**获取邀请信息 */
    getInviteInfo(InviteOpenid) {
        if (!this.data.openid) {
            this.setData({
                spinShow: true
            })
            setTimeout(() => {
                this.getUserOpenid()
                this.getInviteInfo(InviteOpenid)
            }, 1000)
            return
        } else {
            if (this.data.spinShow) {
                this.setData({
                    spinShow: false
                })
            }
        }
        requestUtil.getRequest(urls.leader.joinEmployess + InviteOpenid + '/' + this.data.openid, {}, (res) => {
            this.setData({
                joinInfo: res.resData,
                inviteOpenid: InviteOpenid,
                userStatus: res.resCode
            })
        }, () => {})
    },

    /**
     * 确认加入
     */
    employeeToJoin() {
        if (!this.data.openid) {
            this.setData({
                spinShow: true
            })
            setTimeout(() => {
                this.getUserOpenid()
                this.employeeToJoin()
            }, 1000)
            return
        } else {
            if (this.data.spinShow) {
                this.setData({
                    spinShow: false
                })
            }
        }
        requestUtil.postRequest(urls.leader.employeeToJoin + this.data.inviteOpenid, {
            'openid': this.data.openid
        }, (res) => {
            wx.showModal({
                title: '恭喜您',
                content: '您已成功加入\“' + this.data.joinInfo.companyName + '\”团队',
                showCancel: false,
                success: res => {
                    wx.switchTab({
                        url: '/pages/user/personal/index'
                    })
                }
            })
        }, () => {})
    },

    /**获取openid */
    getUserOpenid() {
        let openid = getApp().getUserOpenid()
        this.setData({
            openid
        })
    },

    /**关闭此页面 */
    closePage() {
        wx.switchTab({
            url: '/pages/user/personal/index'
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getUserOpenid()
        let inviteOpenid = options.inviteOpenid || options.scene
        this.getInviteInfo(inviteOpenid)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})